package com.oocl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.oocl.pojo.OrderInfo;
import com.oocl.pojo.User;

/**
 * Servlet implementation class ViewCartServlet
 */
public class ViewCartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(ViewCartServlet.class);
       
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		logger.info(((User)request.getSession().getAttribute("user")).getUsername()+" view his(her) cart");
		Map<String, OrderInfo> carts = (Map<String, OrderInfo>) request.getSession().getAttribute("cart");
		if(null==carts){
			request.setAttribute("result", "你未选择任何商品!");
			request.getRequestDispatcher("store").forward(request, response);
			return;
		}
		List<OrderInfo> orders = new ArrayList<>();
		for(String s:carts.keySet()){
			orders.add(carts.get(s));
		}
		request.setAttribute("orders", orders);
		request.getRequestDispatcher("../WEB-INF/cart.jsp").forward(request, response);
	}

}
